cmake_minimum_required(VERSION 2.8)

#project name
project(client)

# Set output path for libraries.
set(LIBRARY_OUTPUT_PATH ${client_BINARY_DIR}/lib CACHE PATH "Output directory for the client libraries.")

# Set output path for binaries.
set(EXECUTABLE_OUTPUT_PATH ${client_BINARY_DIR}/bin CACHE PATH "Output directory for the client executables.")

# Add path to client include directories.
include_directories(${PROJECT_SOURCE_DIR}/client/include/)
include_directories(${PROJECT_SOURCE_DIR}/../shared_code/include)

# Set default build type.
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: Release and Debug." FORCE)
endif(NOT CMAKE_BUILD_TYPE)

option(BUILD_SHARED_LIBS "Build Shared Libraries." ON)

if(UNIX)
  add_definitions(-DLINUX)
  add_definitions(-D_LINUX)
#  add_definitions(-DDEBUG_INFO)
  add_definitions(-DCLIENT)
  set(CMAKE_C_FLAGS_DEBUG "-g -ggdb")
  set(CMAKE_C_FLAGS_RELEASE "-O2")
endif(UNIX)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  add_definitions(-DDEBUG)
  add_definitions(-D_DEBUG)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

################################################################################
# boost configuration.
################################################################################
find_package(Boost COMPONENTS system filesystem thread signals REQUIRED)
if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
  set(client_Boost ${Boost_SYSTEM_LIBRARY}
                   ${Boost_FILESYSTEM_LIBRARY} 
                   ${Boost_SIGNALS_LIBRARY} 
                   ${Boost_THREAD_LIBRARY})
endif()

find_package(FreeGlut REQUIRED)
if(FREEGLUT_FOUND)
  include_directories(${FREEGLUT_INCLUDE_DIRS})
  set(client_freeglut ${FREEGLUT_LIBRARIES})
endif()

find_package(Eigen3 REQUIRED)
if(EIGEN3_FOUND)
  include_directories(${EIGEN3_INCLUDE_DIR})
endif()

find_package(OpenGL REQUIRED)
if(OPENGL_FOUND)
  include_directories(${OPENGL_INCLUDE_DIR})
  set(client_opengl ${OPENGL_LIBRARIES})
endif()

find_package(LibXml2 REQUIRED)
if(LIBXML2_FOUND)
  include_directories(${LIBXML2_INCLUDE_DIR})
  set(client_libxml ${LIBXML2_LIBRARIES})
endif()

find_package(GLEW REQUIRED)
if(GLEW_FOUND)
  include_directories(${GLEW_INCLUDE_PATH})
  set(client_glew ${GLEW_LIBRARY})
endif()

find_package(JPEG REQUIRED)
if(JPEG_FOUND)
  include_directories(${JPEG_INCLUDE_DIR})
  set(client_jpeg ${JPEG_LIBRARIES})
endif()

find_package(PNG REQUIRED)
if(PNG_FOUND)
  include_directories(${PNG_INCLUDE_DIR})
  set(client_png ${PNG_LIBRARIES})
endif()

# Library name declaration.
macro(client_LIBRARY _LIBRARY_NAME)
  set(client_LIBRARY_NAME client${_LIBRARY_NAME})
  set(client_PROJECT_NAME lib${_LIBRARY_NAME})
  set(client_LIBRARY_SOURCES "")
  set(client_LIBRARY_HEADERS "")
endmacro(client_LIBRARY)

# Macro for adding source file to library build.
macro(client_LIBRARY_SOURCE)
  set(client_LIBRARY_SOURCES ${client_LIBRARY_SOURCES} ${ARGV})
endmacro(client_LIBRARY_SOURCE)

# Macro for adding header file to library.
macro(client_LIBRARY_HEADER)
  set(client_LIBRARY_HEADERS ${client_LIBRARY_HEADERS} ${ARGV})
endmacro(client_LIBRARY_HEADER)

# Macro for adding all header files in a specified directory to library,
macro(client_LIBRARY_INCLUDE_DIR _DIR)
  file(GLOB_RECURSE client_LIBRARY_HEADERS ${_DIR}/*.h ${_DIR}/*.hxx)
endmacro(client_LIBRARY_INCLUDE_DIR)

# Building a library macro.
macro(client_LIBRARY_BUILD)
  add_library(${client_LIBRARY_NAME} ${client_LIBRARY_SOURCES}
    ${client_LIBRARY_HEADERS})
  set_target_properties(${client_LIBRARY_NAME} PROPERTIES
    PROJECT_LABEL ${client_PROJECT_NAME}
    LINK_FLAGS "${client_LINK_FLAGS}")
  #target_link_libraries(${client_LIBRARY_NAME} ${client_LIBRARY_ADDITIONAL_LIBS})
endmacro(client_LIBRARY_BUILD)

# Install macro.
macro(client_LIBRARY_INSTALL)
  install(TARGETS ${client_LIBRARY_NAME}
          RUNTIME DESTINATION bin
          LIBRARY DESTINATION lib
          ARCHIVE DESTINATION lib)
  foreach(HEADER ${client_LIBRARY_HEADERS})
    file(RELATIVE_PATH FILE ${client_SOURCE_DIR}/include ${HEADER})
    get_filename_component(DIR ${FILE} PATH)
    install( FILES ${HEADER} DESTINATION include/${DIR})
  endforeach(HEADER)
endmacro(client_LIBRARY_INSTALL)

# Macro used for declaring an executable program.
macro(client_PROGRAM _PROGRAM_NAME)
  set(client_PROGRAM_NAME ${_PROGRAM_NAME})
  set(client_PROGRAM_SOURCES "")
  set(client_PROGRAM_HEADERS "")
  set(client_PROGRAM_ADDITIONAL_LIBS "")
endmacro(client_PROGRAM)

# Macro for adding source file to executable program build.
macro(client_PROGRAM_SOURCE)
  set(client_PROGRAM_SOURCES ${client_PROGRAM_SOURCES} ${ARGV})
endmacro(client_PROGRAM_SOURCE)

# Macro for adding header file to executable program.
macro(client_PROGRAM_HEADER)
  set(client_PROGRAM_HEADERS ${client_PROGRAM_HEADERS} ${ARGV} )
endmacro(client_PROGRAM_HEADER)

# Macro for adding additional library to executable program build.
macro(client_PROGRAM_ADDITIONAL_LIBRARY)
  set(client_PROGRAM_ADDITIONAL_LIBS ${client_PROGRAM_ADDITIONAL_LIBS}
    ${ARGV})
endmacro(client_PROGRAM_ADDITIONAL_LIBRARY)

# Building executable program macro.
macro(client_PROGRAM_BUILD)
  add_executable(${client_PROGRAM_NAME} ${client_PROGRAM_SOURCES}
    ${client_PROGRAM_HEADERS})
  link_directories(${LIBRARY_OUTPUT_PATH})
  target_link_libraries(${client_PROGRAM_NAME} ${client_LIBS}
    ${client_PROGRAM_ADDITIONAL_LIBS})
endmacro(client_PROGRAM_BUILD)

# Install executable program macro.
macro(client_PROGRAM_INSTALL)
  install(TARGETS ${client_PROGRAM_NAME} RUNTIME DESTINATION bin )
endmacro(client_PROGRAM_INSTALL)

# Continue with subdirectories.
add_subdirectory(client/src)


